var ClientDelegate = require('./../../FrameWork/Delegate/ClientDelegate');
var ClientEveDelegate = require('./../../FrameWork/Delegate/ClientEveDelegate');
var ClientPkDelegateType = require('./../Delegate/ClientPkDelegateType');
var TimeOutCtr = require('./../../FrameWork/Component/TimeOutCtr');
var UtilGameObject = require('./../../FrameWork/Utils/UtilGameObject');
var EnumOpBtn = require('./../Enum/EnumOpBtn');
var _ = require('./../../FrameWork/we_underscore');
cc.Class({
    extends: ClientDelegate,
    properties: {
        timeOutCtr: {
            default: null,
            type: TimeOutCtr,
            displayName: '倒计时组件'
        },
        nodeNoOut: {
            default: null,
            type: cc.Node
        },
        nodeTip: {
            default: null,
            type: cc.Node
        },
        nodeOutCard: {
            default: null,
            type: cc.Node
        },
        nodeYbq: {
            default: null,
            type: cc.Node
        },
        prefabLb: {
            default: null,
            type: cc.Prefab,
            displayName: '文字预设'
        },
        colorNoCanTouch: {
            default: cc.Color.WHITE,
            displayName: '不可点击颜色'
        },
        threeBtnsNode: {
            default: null,
            type: cc.Node,
            displayName: '三按钮试'
        }
    },
    onLoad() {
        this._super();
        this.addListerDelegate(ClientPkDelegateType.Clinet_gamingBtnsState, this.doSetBtnStat.bind(this));
        var posList = [{
            pos: this.nodeNoOut,
            world: '不 出'
        }, {
            pos: this.nodeTip,
            world: '提 示'
        }, {
            pos: this.nodeOutCard,
            world: '出 牌'
        }, {
            pos: this.nodeYbq,
            world: '要不起'
        }];
        _.each(posList, function(_info) {
            var lbCtr = UtilGameObject.getCtrByPrefabs(this.prefabLb, _info.pos);
            lbCtr.setFontSize(43);
            lbCtr.setPositionY(5);
            lbCtr.setText(_info.world);
            lbCtr.setOutlineWidth2(5);
        }.bind(this));
        // this.resetNet();
    },
    resetNet() {
        this.setCanTouchColor(this.nodeNoOut);
        this.setCanTouchColor(this.nodeTip);
        this.setCanTouchColor(this.nodeOutCard);
        var isOutCard = (window.g_room && window.g_room.isSelfOutCard());
        this.setActive(isOutCard);
    },
    doSetBtnStat(_state) {
        let isShow = EnumOpBtn.NONE != _state;
        this.doActive(isShow);
        if (!isShow) {
            this.timeOutCtr.doClose();
            return;
        } else {
            /**倒计时 */
            this.openTimeOut();
        }
        this.nodeNoOut.active = false;
        this.nodeTip.active = false;
        this.nodeOutCard.active = false;
        this.nodeYbq.active = false;

        let timeOut = null;
        /**表示需要自己手动选中牌 */
        if (EnumOpBtn.MUSTCHOICECARD == _state) {
            this.setCanTouchColor(this.nodeNoOut, false);
            this.setCanTouchColor(this.nodeTip, false);
            this.setCanTouchColor(this.nodeOutCard);
        } else if (EnumOpBtn.GET_OTHER_CARD == _state) {
            this.setCanTouchColor(this.nodeNoOut);
            this.setCanTouchColor(this.nodeTip);
            this.setCanTouchColor(this.nodeOutCard);
        } else if (EnumOpBtn.YBQ == _state) {
            this.setCanTouchColor(this.nodeYbq);
            timeOut = 3;
        } else if (EnumOpBtn.AUTOLASTCARD == _state) {
            timeOut = 3;
            this.setCanTouchColor(this.nodeNoOut);
            this.setCanTouchColor(this.nodeTip);
            this.setCanTouchColor(this.nodeOutCard);
            this.outAllCard();
            this.openTimeOut(timeOut, function() {
                ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_autoOutSelfCardAll);
            });
            return;
        }
        if (!!isShow) {
            /**倒计时 */
            this.openTimeOut(timeOut);
        }
    },
    doActive(_is) {
        this.setActive(_is);
        // if (!!_is) {
        //     this.openTimeOut();
        // } else {
        //     this.timeOutCtr.doClose();
        // }
    },
    setCanTouchColor(_node, _isCanTouch) {
        _isCanTouch = null == _isCanTouch ? true : _isCanTouch;
        var _color = _isCanTouch ? cc.Color.WHITE : this.colorNoCanTouch;
        _node.color = _color;
        _node.active = true;
        var lb = _node.getChildByName('Label');
        if (!!lb) {
            lb.color = _color;
        }

        var _btn = _node.getComponent(cc.Button);
        if (!!_btn) {
            _btn.interactable = _isCanTouch;
        }
    },
    registerOutCard(_cb) {
        this.cbOutCard = _cb;
    },
    registerNoOut(_cb) {
        this.cbNoOut = _cb;
    },
    registerTip(_cb) {
        this.cbTip = _cb;
    },
    /*========================================================================================*/
    eveOutCard() {
        var isOutCard = (window.g_room && window.g_room.isSelfOutCard());
        if (false == isOutCard) {
            return;
        }
        if (!!this.cbOutCard)
            this.cbOutCard();
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_gamingBtn_Out);
        // this.doActive(false);
    },
    eveNoOut() {
        if (!!this.cbNoOut)
            this.cbNoOut();
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_gamingBtn_noOut);

        if (window.g_room) {
            window.g_room.selfNoOut();
        }

        this.timeOutCtr.doClose();
        this.doActive(false);
    },
    eveTip() {
        if (!!this.cbTip)
            this.cbTip();
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_gamingBtn_tip);
    },
    /**开启倒计时 */
    openTimeOut(_timeOut, _cb) {
        _timeOut = null == _timeOut ? 20 : _timeOut;
        if (null == _cb) {
            this.timeOutCtr.startCountDown(_timeOut, this.timeOutCallBack.bind(this));
        } else {
            this.timeOutCtr.startCountDown(_timeOut, _cb);
        }
    },
    /***超时处理 */
    timeOutCallBack() {
        //如果是自己必须出牌就自动出最后一张 
        if (!!window.g_room && !!window.g_room.timeOutOutCard) {
            window.g_room.timeOutOutCard();
            return;
        }
        this.eveNoOut();
    }
});